#include <faker/engine/ui/SceneTreeNodeEditor.h>
#include <faker/engine/ui/StateBuffer.h>

#include <faker/basic/uid/Inst.h>
#include <faker/basic/transform/Inst.h>
#include <faker/basic/info/Inst.h>
#include <faker/basic/pch.h>

using namespace Faker::Engine::UI;

void Faker::Engine::UI::SceneTreeNodeEditor::Instance()
{
    if (mOpen)
    {
        if (ImGui::Begin(mTitle.c_str(), &mOpen))
        {
            auto allEntity = m_Scene_ref->m_Registry.view<Faker::Basic::Uid::Inst>();

            for (auto e : allEntity)
            {
                TreeNode(Faker::Entity(m_Scene_ref, e));
            }
        }
        
        ImGui::End();
    }
}

void Faker::Engine::UI::SceneTreeNodeEditor::TreeNode(Faker::Entity e)
{
    auto &uid = e.GetOrEmplaceComponent<Faker::Basic::Uid::Inst>();

    ImGui::PushID(uid.m_UUID);

    auto &info = e.GetOrEmplaceComponent<Faker::Basic::Info::Inst>();
    ImGui::Text("%s", info.m_Name.c_str());
    ImGui::SameLine();

    if (ImGui::SmallButton("clicked"))
    {

        auto &sg = Faker::Engine::UI::StateBuffer::signal;
        sg._shouldChangeEntity = true;
        sg._entityValue = e;
    }

    ImGui::PopID();
}

void  Faker::Engine::UI::SceneTreeNodeEditor::HandleSignal()
{

}